home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The 640 MEG Shareware Studio 2
/
The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO
/
clang
/
dump1a.zip
/
DUMP.C
next >
Wrap
C/C++ Source or Header
|
1990-09-13
|
4KB
|
117 lines
/* ---------- DUMP.C
---------- Placed in the public domain on 25 August 1990 by
---------- Richard M. Utter, ProLogic
---------- This is an alternative to the file dump capability provided by
---------- DOS's DEBUG. It was hastily batted out after I grew tired of
---------- DEBUG's inability to provide proper values for file offsets and
---------- to display anything other than bytes. You are warned: DUMP is far
---------- from perfect. It's fairly crummy code. But it gets the job as I
---------- defined it (dump longwords and ASCII with correct file offsets)
---------- done.
---------- Command line: DUMP file_name
---------- After the initial data appears, press any key other than <ESC> to
---------- see more. Press <ESC> any time to exit. Note that DUMP substitutes
---------- periods for control characters from 0 to 0x1f in the righthand
---------- ASCII dump.
*/
#include <conio.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* ---------- Define display lines and the buffer size in terms of both
---------- characters and longwords.
*/
#define nlines 16
#define clength nlines * 16
#define llength nlines * 4
/* ---------- Executable statements */
main (int argc, char * argv[])
{
FILE * infile = NULL;
unsigned char * ucp, * xcp;
unsigned char string[17];
int lines, i, j;
unsigned long int buffer[llength], offset = 0;
size_t chars_read;
string[16] = '\0';
/* ---------- Drop dead if file name is missing from command line. */
if (argc < 2)
{
printf ("\nUse \"DUMP file_name\"\n");
return 1;
}
infile = fopen (argv[1], "rb");
/* ---------- Bail out if file name won't open. */
if (infile == NULL)
{
printf ("File open error...");
return 2;
}
/* ---------- Finally: dump the data. */
while ((!feof (infile)) && (!ferror (infile)))
{
/* ---------- Get "clength" characters. */
chars_read = fread (buffer, (size_t) 1, (size_t) (clength), infile);
/* ---------- Point at start of buffer. */
ucp = (unsigned char *) buffer;
/* ---------- How many full lines are there? */
lines = chars_read / 16;
printf ("\n F E D C B A 9 8 7 6 5 4 3 2 1 0\n");
for (i = 0; i < lines; i++)
{
/* ---------- Hex dump. */
for (j = 3; j >= 0; j--)
printf ("%8.8lx ", buffer[i * 4 + j]);
/* ---------- ASCII dump. */
memcpy (string, ucp, (size_t) 16);
for (j = 0; j < 16; j++, ucp++)
if (string[j] < 32)
string[j] = '.';
printf (" %s %8.8lx %ld\n", string, offset, offset);
offset += 16;
}
/* ---------- Deal with possible final truncated line. */
i = chars_read % 16;
if (i)
{
/* ---------- Point to correct location in buffer. */
xcp = ucp;
for (j = 1; j < i; j++, xcp++)
;
/* ---------- Print blanks for any missing bytes. */
for (j = 1; j <= 16 - i; j++)
{
if ((j == 4) || (j == 8) || (j == 12))
printf (" ");
printf (" ");
}
/* ---------- Hex dump. */
for (j = i; j > 0; j--, xcp--)
{
if ((j != i) && (j % 4 == 0))
printf (" ");
printf ("%2.2x", *xcp);
}
printf (" ");
/* ---------- ASCII dump. */
memcpy (string, ucp, (size_t) i);
for (j = 0; j < i; j++)
if (string[j] < 32)
string[j] = '.';
for (j =i; j < 16; j++)
string[j] = ' ';
printf (" %s %8.8lx %ld\n", string, offset, offset);
}
printf ("\n");
/* ---------- Get operator input. Any character other than <ESC> displays
---------- next "clength" bytes.
*/
i = getch ();
if (i == 27)
break;
}
fclose (infile);
return EXIT_SUCCESS;
}